package org.research.dao;
import java.io.Serializable;
import java.sql.Connection;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/** 统一数据访问接口实现 */
public class BaseDAOImpl extends HibernateDaoSupport implements BaseDAO {
	
	/** 
	 * 统计指定类的所有持久化对象 
	 * (TODO: 得出的结果是Integer类型的，无法转化到Long型)
	 */
	public int countAll(String clazz) {
		final String hql = "select count(*) from "+clazz+ " as a";
		Long count = (Long)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(hql);
				query.setMaxResults(1);
				return query.uniqueResult();
			}
		});	
		return count.intValue();
	}

	/**
	 * 统计指定类的查询结果  
	 * (TODO: 得出的结果是Integer类型的)
	 */
	public int countQuery(String hql) {
		final String counthql = hql;
		Integer count = (Integer)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(counthql);
				query.setMaxResults(1);
				return query.uniqueResult();
			}
		});
		return count.intValue();
	}

	/** 删除指定ID的持久化对象 */
	public void delById(Class clazz,Serializable id) {
		getHibernateTemplate().delete(getHibernateTemplate().load(clazz, id));
	}

	/** 装载指定类的所有持久化对象 */
	public List listAll(String clazz) {
		return getHibernateTemplate().find("from "+clazz+" as a order by a.id");
	}
	
	/** 分页装载指定类的所有持久化对象 */
	public List listAll(String clazz, int pageNo, int pageSize) {
		final int pNo = pageNo;
		final int pSize = pageSize;
		final String hql = "from "+clazz+ " as a order by a.id desc";
		List list = getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(hql);
				query.setMaxResults(pSize);
				query.setFirstResult((pNo-1)*pSize);
				List result = query.list();
				if (!Hibernate.isInitialized(result))Hibernate.initialize(result);
				return result;
			}
		});	
		return list;
	}
	
	/** 加载指定ID的持久化对象 */
	public Object loadById(Class clazz,Serializable id) {
		return getHibernateTemplate().get(clazz, id);
	}
	
	/**加载满足条件的持久化对象*/
	public Object loadObject(String hql) {
		final String hql1 = hql;
		Object obj = null;
		List list = getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(hql1);
				return query.list();
			}
		});			
		if(list.size()>0)obj=list.get(0);	
		return obj;
	}

	/** 查询指定类的满足条件的持久化对象 */
	public List query(String hql) {
		final String hql1 = hql;
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(hql1);
				return query.list();
			}
		});	
	}

	/** 分页查询指定类的满足条件的持久化对象 */
	public List query(String hql, int pageNo, int pageSize) {
		final int pNo = pageNo;
		final int pSize = pageSize;
		final String hql1 = hql;
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(hql1);
				query.setMaxResults(pSize);
				query.setFirstResult((pNo-1)*pSize);
				List result = query.list();
				if (!Hibernate.isInitialized(result))Hibernate.initialize(result);
				return result;
			}
		});	
	}

	/** 保存或更新指定的持久化对象 */
	public void saveOrUpdate(Object obj) {
		getHibernateTemplate().saveOrUpdate(obj);
	}

	/** 条件更新数据 */
	public int update(String hql) {
		final String hql1 = hql; 
		return ((Integer)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(hql1);
				return query.executeUpdate();
			}
		})).intValue();	
	}
	
	/** 从连接池中取得一个JDBC连接 */
	public Connection getConnection() {
		return getHibernateTemplate().getSessionFactory().getCurrentSession().connection();
	}
	
	/**
	 *  备份数据库操作
	 */
	public boolean  bakDB(String strSQL){

		boolean isResult = true;
		Session session = null;
		Transaction transaction = null;
		try{
			//System.out.println("开始数据备份");
			session  = getHibernateTemplate().getSessionFactory().openSession();
			transaction = session.beginTransaction();
			// SQL语句
			//String strSQL = "mysqldump -u" + strUserName + " –p" + strPassword +" " +strDBName +" > "+strBakName;
			session.createSQLQuery(strSQL);
			//System.out.println("数据备份完成");
		} catch(Exception e) {
			transaction.rollback();
			//System.out.println("数据备份出错");
			isResult = false;
		} finally {
			if(session!=null){
				session.close();
			}
		}
		return isResult;
	} 
	
	
	/**
	 * 还原数据库
	 */
	public boolean restoreDB(String strSQL){
		
		boolean isResult = true;
		Session session = null;
		Transaction transaction = null;
		try{
			//System.out.println("开始数据还原");
			session  = getHibernateTemplate().getSessionFactory().openSession();
			transaction = session.beginTransaction();
			session.createSQLQuery(strSQL);
			//System.out.println("数据还原完成");
		} catch(Exception e) {
			transaction.rollback();
			//System.out.println("数据备份出错");
			isResult = false;
		} finally {
			if(session!=null){
				session.close();
			}
		}
		return isResult;
		
	} 
	
	/**
	 * 删除
	 */
	public boolean delete(String hql) {
		Session session = null;
		Transaction tx = null;
		boolean blnResult =  true;
		try {
			session = this.getAppSession();
			tx = session.beginTransaction();
			session.createQuery(hql);
			tx.commit();
		} catch (Exception e) {
			if (tx != null && tx.isActive() && !tx.wasRolledBack()) {
				tx.rollback();
			}
			blnResult =  false;
			logger.error(e.getMessage(), e);
		} finally {
			if (tx != null && tx.isActive() && !tx.wasCommitted()
					&& !tx.wasRolledBack()) {
				tx.commit();
			}
			blnResult =  false;
		}
		return blnResult;
	}
	
	/**
	 * 获取当前的SESSION对象;
	 * 
	 * @return
	 */
	public Session getAppSession() {
		return this.getSessionFactory().getCurrentSession();
	}

}